﻿using System;
using System.Collections.Generic;
using System.Text;
using AppleAutoBuy.Model;
using System.Xml.Serialization;
using System.IO;
using System.Xml;

namespace AppleAutoBuy.Common
{
    public class AppleSerializerUtil
    {      

        #region Methods

        public static AppleData DeserializeAppleData(string fileName)
        {
            AppleData appleData = new AppleData();

            XmlSerializer serializer = new XmlSerializer(typeof(AppleData));
            using (StreamReader sReader = new StreamReader(fileName))
            {
                XmlReader xReader = XmlReader.Create(sReader);
                if (serializer.CanDeserialize(xReader))
                {
                    appleData = serializer.Deserialize(xReader) as AppleData;
                }
                sReader.Close();
            }

            return appleData;
        }

        public static void SerializeAppleDataToFile(AppleData appleData,string fileName)
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(AppleData));
            using (StreamWriter sWriter = new StreamWriter(fileName,false))
            {
                xmlSerializer.Serialize(sWriter, appleData);
                sWriter.Close();
            }
        }

        #endregion

    }


}
